package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@Controller
@ComponentScan
public class DemoApplication {

    @Autowired
	private  UserDao userDao;


	@RequestMapping(value = "/")
	public String were(String name ){
		/*UserPo userPo = new UserPo();
		userPo.setName(name);
		UserPo po = userDao.save(userPo);
		if(po !=null){*/
			return "crud";
		/*}*/

		//return null;
	};

	@RequestMapping(value = "home/{userId}")
	public String  home(@PathVariable("userId") String userId ){
        long id;
		id = Long.valueOf(userId);
		UserPo user = userDao.findByUserId(id);
		List<UserPo> userAll = userDao.findAll();
		//userDao.delete(user);
       // System.out.println(flag);
        UserPo userPo = new UserPo();
		userPo.setName("asdsad");
		UserPo po = userDao.save(userPo);
		System.out.println(po.getUserId());
		int upflag = userDao.update("wwwwsxx",id);
        System.out.println(upflag);
		String k= "-100";
		System.out.println(Integer.parseInt(k));
		if(user!=null){
//			ModelAndView mav = new ModelAndView("webapp/WEB-INF/views/crud.html");
			return "angulerJs";
		}
		if(userAll !=null){
			String name = "";
			for (int i = 0 ;i<userAll.size() ;i++){
				name +=""+userAll.get(i).getName();
				System.out.println(userAll.get(i).getName()+"   "+userAll.get(i).getUserId());
			}
			return "angulerJs";
		}
		return "angulerJs";
	}

	@ResponseBody
	@RequestMapping(value = "allUser" , method = RequestMethod.POST)
	public List<UserPo>  allUser(){
		List<UserPo> userAll = userDao.findAll();
		//userDao.out.println(flag);
		if(userAll !=null && userAll.size() > 0){

			return userAll;
		}
		return null;
	}

	@ResponseBody
	@RequestMapping(value = "user",method = RequestMethod.POST)
	public List<UserPo> findUser(String userId ){
		long id;
		id = Long.valueOf(userId);
		UserPo user = userDao.findByUserId(id);
		List<UserPo> userList = new ArrayList<UserPo>() ;
		if(user!=null){
//			ModelAndView mav = new ModelAndView("webapp/WEB-INF/views/crud.html");
			System.out.println(user.getUserId());
			System.out.println(user.getName());
			userList.add(user);
			return userList;
		}

		return null;
	}

	@ResponseBody
	@RequestMapping(value = "addUser",method = RequestMethod.POST)
	public String addUser(String name ){
		UserPo userPo = new UserPo();
		userPo.setName(name);
		UserPo po = userDao.save(userPo);
		if(po !=null){
			return "ok";
		}

		return null;
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
